/*
 * Copyright (C), 2014-2015, 杭州小卡科技有限公司
 * FileName: CicpServiceProxy.java
 * Author:   muxing
 * Date:    2015/10/16 15:06
 * Description:
 */
package com.xiaoka.ins.corp.api.cicp.proxy;

import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.xiaoka.ins.corp.api.cicp.dto.CicpEnum;
import com.xiaoka.ins.corp.api.cicp.dto.ReqEntity;
import com.xiaoka.ins.corp.api.cicp.dto.RespEntity;
import com.xiaoka.ins.corp.api.cicp.dto.base.Base;
import com.xiaoka.ins.corp.api.cicp.dto.base.Head;
import com.xiaoka.ins.corp.api.cicp.dto.base.Main;
import com.xiaoka.ins.corp.api.cicp.dto.base.Vhl;
import com.xiaoka.ins.corp.api.config.CicpConfig;
import com.xiaoka.ins.corp.common.dto.VehicleInfoParameter;
import com.xiaoka.ins.corp.common.dto.base.InsParameter;

/**
 * CicpServiceProxy
 *
 * @author muxing
 * @date 2015/10/16
 */
public class CicpServiceUtils {

	private final static Logger log = LoggerFactory.getLogger(CicpServiceUtils.class);

	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private ReqEntity reqEntity;

	private XStream xStream;

	public static final String headerXml = "<?xml version='1.0' encoding='GBK'?>";

	public CicpServiceUtils(CicpConfig cicpConfig) {
		this.reqEntity = new ReqEntity();

		Head head = new Head();
		reqEntity.setHEAD(head);
		head.setUSER_NAME(cicpConfig.getUserName());
		head.setUSER_PSW(cicpConfig.getUserPwd());

		Main main = new Main();
		reqEntity.setMAIN(main);
		main.setTRANSRDATE(sdf.format(new Date()));
		main.setCHANNELCODE(cicpConfig.getChannelCode());

		Base base = new Base();
		reqEntity.setBASE(base);
	}

	public RespEntity handle() {
		xStream = new XStream(new XppDriver(new XmlFriendlyReplacer("_-", "_")));
		xStream.processAnnotations(RespEntity.class);
		xStream.autodetectAnnotations(true);
		String responseXml = null;
		String requestXml = headerXml + xStream.toXML(reqEntity);
		log.debug("cicp requestXml string:/n" + requestXml);
		try {
			responseXml = CicpProxy.getInstance().getRequest(requestXml);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		log.debug("cicp responseXml string:/n" + responseXml);
		RespEntity respEntity = (RespEntity) xStream.fromXML(responseXml);
		return respEntity;
	}

	public void setVehicleInfo(InsParameter insParameter) {
		VehicleInfoParameter vehicleInfoParameter = (VehicleInfoParameter) insParameter;
		reqEntity.getHEAD()
		         .setBUSINESS_CODE(CicpEnum.BUSINESS_CODE.parseType(insParameter.getInsType()).getCode());// 交强险和商业险13
		reqEntity.getMAIN().setSERIALDECIMAL("2015101900001111");// 唯一交易码
		reqEntity.getBASE().setC_DPT_CDE(insParameter.getCityCode());// 城市地区代码

		reqEntity.getMAIN().setTRANSRNO("1031");// 业务类型
		Vhl vhl = new Vhl();
		reqEntity.setVHL(vhl);
		vhl.setC_LCN_NO(vehicleInfoParameter.getVehicleNo());// 车牌号，新车未上牌*-*
		vhl.setC_ENG_NO(vehicleInfoParameter.getVehicleEngNo());// 大于等于6位
		vhl.setC_VHL_FRM(vehicleInfoParameter.getVehicleVim());// 17位
		vhl.setC_BRND_CDE(vehicleInfoParameter.getVehicleBrand());// 品牌型号（无牌车辆必传,北京地区有牌车不用传）
		vhl.setC_DRV_OWNER(vehicleInfoParameter.getVehicleOwner());// 车主姓名(北京必传)
		vhl.setC_FST_REG_DTE(vehicleInfoParameter.getVehicleRegDte());// 精确到天YYYY-MM-DD新车可为空

	}

	public ReqEntity getReqEntity() {
		return reqEntity;
	}

	public void setReqEntity(ReqEntity reqEntity) {
		this.reqEntity = reqEntity;
	}

}
